package com.firefightingiot.firefightingiot.web.web;

import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.common.base.BaseController;
import com.firefightingiot.firefightingiot.common.utils.appkit.ConfigKit;
import com.firefightingiot.firefightingiot.support.annotaion.Action;
import com.firefightingiot.firefightingiot.support.annotaion.At;
import com.firefightingiot.firefightingiot.support.annotaion.Import;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.web.service.AlarmService;
import com.firefightingiot.firefightingiot.web.service.FireService;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

/**
 * 消防物联网报警控制器
 */
@Action
@RequestMapping(value="alarmInfo")
public class AlarmController extends BaseController {
    @Resource
    public AlarmService alarmService;
    @Resource
    public FireService fireService;
    /**
     * 处置功能
     * @param
     * id    单位设备详细日志表的主键id	否
     * handlePerson	处理人员	否
     * infoconfirm	信息确认：1 报警 2 误报 3 测试	否
     * handleRemark	处理备注	否
     * ids	分模块的日志id数组	否
     * number	设备编号	否
     * alarmType	报警类型:对应字典表报警类型	否
     * handletime	处理时间	否
     * @return
     */


    /**
     * 报警日志添加
     * @param
     * number    设备编号	否
     * content	报警内容	否
     * alarmType	报警类型:对应字典表报警类型	否
     * alarmTypeName	报警类型名称	否
     * id	主键id	是
     * path	视频地址	否
     * alarmtime	报警时间	否
     * @return
     */
    @At(value = "add", method = RequestMethod.POST)
    public RespEntity add(@RequestBody(required = false) JSONObject jsonParam) throws ParseException, InterruptedException {
        return alarmService.add(jsonParam);
    }

    /**
     * 处置模块分类功能(总历史记录点处置时触发)
     * @param
     * id    单位设备详细日志表的主键id	是
     * handlePerson	处理人员	否
     * infoconfirm	信息确认：1 报警 2 误报 3 测试	是
     * batchHandle	是否批处理：是：true  不是：false	是
     * handleRemark	处理备注	否
     * deviceClassify	设备分类:1 电气火灾 2 消防用水 3 烟感 4 可燃气体 5 火灾自动联网 6 消防可视化	是
     * number	设备编号	是
     * handletime	处理时间	否
     * @return
     */
    @At(value = "typeHandle", method = RequestMethod.POST, param = { "id","batchHandle","deviceClassify"})
    public RespEntity typeHandle(@RequestBody(required = false) JSONObject jsonParam) throws InterruptedException, ParseException {
        return alarmService.typeHandle(jsonParam);
    }

    /**
     * @Author YangYu
     * @Description 一键异步处理报警日志
     * @Date Created in 10:58 2022/5/7
     * @Param [jsonParam]
     */

    @At(value = "oneClickHandle", method = RequestMethod.POST)
    public RespEntity oneClickHandle(@RequestBody(required = false) JSONObject jsonParam) throws InterruptedException, ParseException {
        return alarmService.oneClickHandle(jsonParam);
    }

    /**
     * 更多(历史)报警信息列表查询
     * @param
     *
     * @return
     */
    @At(value = "alarmInfoListPage", method = RequestMethod.GET, param = { "page","limit"})
    public RespEntity alarmInfoListPage(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.alarmInfoListPage(jsonParam);
    }

    /**
     * 报警信息详情
     * @param jsonParam
     * @return
     */
    @At(value = "detail", method = RequestMethod.GET, param = { "id"})
    public RespEntity detail(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.detail(jsonParam);
    }
    /**
     * @return
     * @Title: getTemplte <br>
     * @Description: (下载模板) <br>
     * @author 石延杰<br>
     */
    @At(value = "getTemplte", method = RequestMethod.GET)
    public RespEntity getTemplte(@RequestBody Map<String, Object> params) {
        return alarmService.getTemplte(params);
    }

    /**
     * @return
     * @Title: upload <br>
     * @Description: (导入) <br>
     * @author 石延杰<br>
     */
    @Import(value = "file", startLine = 1, titleLine = 0)
    @At(value = "Upload", method = RequestMethod.POST)
    public RespEntity upload(@RequestBody Map<String, Object> params) throws Exception {
        return alarmService.addUpload(params);
    }

    @RequestMapping(value = "/downloadExcel")
    public ResponseEntity<byte[]> downloadExcel(@RequestParam("fileName") String fileName) throws IOException {
        String fileSavePath = ConfigKit.getStr("excelSavePath", "excel");
        String path = fileSavePath + fileName;
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        //下载显示的文件名，解决中文名称乱码问题
        String downloadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
        //通知浏览器以attachment（下载方式）打开图片
        headers.setContentDispositionFormData("attachment", downloadFielName);
        //application/octet-stream ： 二进制流数据（最常见的文件下载）。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                headers, HttpStatus.CREATED);
    }

    /**
     * 字典组查询
     * @param
     *
     * @return
     */
    @At(value = "dictList", method = RequestMethod.GET)
    public RespEntity dictList(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.dictList(jsonParam);
    }

    /**
     * 报警类型分组名称
     * @param
     *
     * @return
     */
    @At(value = "getAlarmTypeGroupName", method = RequestMethod.GET)
    public RespEntity getAlarmTypeGroupName(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.getAlarmTypeGroupName(jsonParam);
    }

    /**
     * 前端是否对这个用户做报警处理
     * @param
     *
     * @return
     */
    @At(value = "isAlarm", method = RequestMethod.GET)
    public RespEntity isAlarm(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.isAlarm(jsonParam);
    }

    /**
     * 烟感报警消音
     * @param jsonParam
     * @return
     */
    @At(value = "alarmSilencer", method = RequestMethod.POST)
    public RespEntity alarmSilencer(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.alarmSilencer(jsonParam);
    }

    /**
     * 烟感报警消音
     * @param jsonParam
     * @return
     */
    @At(value = "test", method = RequestMethod.POST)
    public RespEntity test(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.test(jsonParam);
    }

    /**
     * @Author YangYu
     * @Description 消防支队报警处置
     * @Date Created in 10:02 2022/10/8
     * @Param [jsonParam]
     */

    @At(value = "restFileHandle", method = RequestMethod.POST)
    public RespEntity restFileHandle(@RequestBody(required = false) JSONObject jsonParam) throws ParseException, InterruptedException {
        return fireService.restFile(jsonParam);
    }

    /**
     * @Author YangYu
     * @Description 消防支队报警恢复
     * @Date Created in 10:02 2022/10/8
     * @Param [jsonParam]
     */

    @At(value = "restRecoverFile", method = RequestMethod.POST)
    public RespEntity restRecoverFile(@RequestBody(required = false) JSONObject jsonParam) throws ParseException, InterruptedException {
        return fireService.restRecoverFile(jsonParam);
    }

    /**
     * @Author YangYu
     * @Description 查询是否有未处置的报警信息
     * @Date Created in 10:26 2023/2/21
     * @Param [jsonParam]
     */

    @At(value = "alarmCount", method = RequestMethod.GET)
    public RespEntity alarmCount(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.alarmCount(jsonParam);
    }


    /**
     * @Author YangYu
     * @Description 报警事件查询
     * @Date Created in 10:26 2023/2/21
     * @Param [jsonParam]
     */

    @At(value = "eventList", method = RequestMethod.GET)
    public RespEntity eventList(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.eventList(jsonParam);
    }

    /**
     * @Author YangYu
     * @Description 报警日历数据
     * @Date Created in 10:26 2023/2/21
     * @Param [jsonParam]
     */

    @At(value = "alarmCalendarList", method = RequestMethod.GET)
    public RespEntity alarmCalendarList(@RequestBody(required = false) JSONObject jsonParam) {
        return alarmService.alarmCalendarList(jsonParam);
    }

}
